using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

using YouTube.API;

namespace Task
{
    /// <summary>
    /// Video Task.
    /// </summary>
    public class VideoTask
    {
        /// <summary>
        /// Gets the video by ID.
        /// </summary>
        /// <param name="videoId">The video id.</param>
        /// <returns></returns>
        public YouTube.API.VideoDetails.VideoDetails GetVideoByID(string videoId)
        {
            string devId = ConfigurationManager.AppSettings["DeveloperID"];
            
            YouTubeService youTube = new YouTubeService();

            YouTube.API.VideoDetails.UTResponse resultSet = youTube.VideosGetDetails(devId, videoId);

            YouTube.API.VideoDetails.VideoDetails vid = resultSet.videoDetails;

            return vid;
        }

        /// <summary>
        /// Searches for videos.
        /// </summary>
        /// <param name="searchText">The search text.</param>
        /// <param name="page">The page.</param>
        /// <returns></returns>
        public YouTube.API.VideoList.video[] SearchForVideos(string searchText, int pageIndex)
        {
            string devId = ConfigurationManager.AppSettings["DeveloperID"];
            
            YouTubeService youTube = new YouTubeService();

            YouTube.API.VideoList.UTResponse resultSet = youTube.VideosListByTag(devId, searchText, pageIndex, 5);

            YouTube.API.VideoList.video[] videos = resultSet.video_list;

            if (videos.Length == 0 && pageIndex > 1)
            {
                return SearchForVideos(searchText, pageIndex - 1);
            }
            else
            {
                return videos;
            }
        }

    }
}
